package zhongbo.java.air.dictionary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AirAlertZoneMapper;
import zhongbo.java.air.dictionary.model.AirAlertZone;
import zhongbo.java.air.dictionary.service.AirAlertZoneService;

import java.util.List;
import java.util.Optional;

/**
 * 警戒区域配置表(AirAlertZone)表服务实现类
 *
 * @author makejava
 * @since 2025-03-31 14:06:00
 */
@Service("airAlertZoneService")
public class AirAlertZoneServiceImpl implements AirAlertZoneService<AirAlertZone> {
    @Autowired
    private AirAlertZoneMapper airAlertZoneMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param entityId 主键
     * @return 实例对象
     */
//    @Override
//    public AirAlertZone queryById(String id) throws Exception {
//        if (id == null){
//            throw new Exception("id不能为空");
//        }
//        Optional<AirAlertZone> airAlertZoneOptional = Optional.ofNullable(airAlertZoneMapper.selectById(id));
//        if (!airAlertZoneOptional.isPresent()){
//            throw new Exception("id不存在");
//        }
//        return airAlertZoneOptional.get();
//    }
    @Override
    public AirAlertZone findOne(String entityId) throws Exception {
        if (entityId == null){
            throw new Exception("id不能为空");
        }
        Optional<AirAlertZone> airAlertZoneOptional = Optional.ofNullable(airAlertZoneMapper.selectById(entityId));
        if (!airAlertZoneOptional.isPresent()){
            throw new Exception("id不存在");
        }
        return airAlertZoneOptional.get();
    }


    @Override
    public PageVo<AirAlertZone> findPage(int page, int pageSize, String keyword, String fields, String oderByField, boolean orderByAsc) {
        QueryWrapper<AirAlertZone> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field),keyword);
            }
        }
        IPage<AirAlertZone> pageParam = new Page<>(page, pageSize);
        IPage airAlertZoneIPage = airAlertZoneMapper.selectPage(pageParam,queryWrapper);
        return new PageVo<>(airAlertZoneIPage);
    }

    /**
     * 新增数据
     *
     * @param entity 实例对象
     * @return 实例对象
     */
//    @Override
//    public AirAlertZone insert(AirAlertZone airAlertZone) throws Exception {
//        if (airAlertZone == null){
//            throw new Exception("新增失败");
//        }
//
//        // 字段重复验证
//        validateUniqueFields(airAlertZone);
//
//        this.airAlertZoneMapper.insert(airAlertZone);
//        return airAlertZone;
//    }
    @Override
    public AirAlertZone add(AirAlertZone entity) throws Exception {
        if (entity == null){
            throw new Exception("新增失败");
        }

        // 字段重复验证
        validateUniqueFields(entity);

        this.airAlertZoneMapper.insert(entity);
        return entity;
    }

    /**
     * 修改数据
     *
     * @param edit 实例对象
     * @return 实例对象
     */
//    @Override
//    public AirAlertZone update(AirAlertZone airAlertZone) throws Exception {
//        if (airAlertZone == null){
//            throw new Exception("更新失败");
//        }
//
//        // 字段重复验证
//        validateUniqueFields(airAlertZone);
//
//        this.airAlertZoneMapper.updateById(airAlertZone);
//        return this.queryById(airAlertZone.getId());
//    }
    @Override
    public AirAlertZone edit(AirAlertZone edit) throws Exception {
        if (edit == null){
            throw new Exception("更新失败");
        }

        // 字段重复验证
        validateUniqueFields(edit);

        this.airAlertZoneMapper.updateById(edit);
        return this.findOne(edit.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param entityId 主键
     * @return 是否成功
     */
//    @Override
//    public boolean deleteById(String id) throws Exception {
//        if (id == null){
//            throw new Exception("id不能为空");
//        }
//        return this.airAlertZoneMapper.deleteById(id) > 0;
//    }
    @Override
    public boolean delete(String entityId) throws Exception {
        if (entityId == null){
            throw new Exception("id不能为空");
        }
        return this.airAlertZoneMapper.deleteById(entityId) > 0;
    }

    @Override
    public List<AirAlertZone> queryAll() {
        if (airAlertZoneMapper.selectList(null) == null){
            return null;
        }
        return airAlertZoneMapper.selectList(null);
    }

    @Override
    public String getCoordinates(String id) {
        if (id == null){
            return null;
        }
        return airAlertZoneMapper.selectById(id).getVertices();
    }

    @Override
    public String getType(String id) {
        if (id == null){
            return null;
        }
        return airAlertZoneMapper.selectById(id).getZoneType();
    }

    // 添加字段重复验证方法
    private void validateUniqueFields(AirAlertZone airAlertZone) throws Exception {
        Optional<AirAlertZone> existingZone = Optional.ofNullable(airAlertZoneMapper.selectByZoneCode(airAlertZone.getZoneCode()));
        if (existingZone.isPresent()){
            throw new Exception("区域业务编号已存在");
        }
    }
}
